<template>
  <div class="content" v-loading="ishow">
    <el-card>
      <el-row>
        <el-col :span="6">
          <span class="strong">学号:</span>
          <el-input v-model="params.sno" placeholder="请输入学号" style="width: 230px;" size="small" clearable></el-input>
        </el-col>
        <el-col :span="6">
          <span class="strong">姓名:</span>
          <el-input v-model="params.name" placeholder="请输入学生姓名" style="width: 230px;" size="small" clearable></el-input>
        </el-col>
        <el-col :span="6">
          <span class="strong">手机号:</span>
          <el-input v-model="params.phone" placeholder="请输入手机号" style="width: 230px;" size="small" clearable></el-input>
        </el-col>
        <el-col :span="6">
          <span class="strong">职务:</span>
          <!-- stuDuties -->
          <el-select v-model="params.stuDuties" placeholder="请选择职务" style="width: 180px;" size="small" clearable>
            <el-option v-for="item in stuDutiesOption" :key="item.id" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-col>
      </el-row>
      <el-row style="margin-top: 10px;">
        <el-col :span="6">

          <span class="strong">班级:</span>
          <el-select v-model="params.classId" placeholder="请选择班级" style="width: 200px;" size="small" clearable>
            <el-option v-for="item in ClassOptin" :key="item.id" :label="item.claName" :value="item.id">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="6">

          <span class="strong">家长名称:</span>
          <el-input v-model="params.familyName" placeholder="请输入家长" style="width: 230px;" size="small"
            clearable></el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" style="margin: 0 10px;" @click="getList" size="small"
            v-if="$hasBP('btn.Studen.select')">查询</el-button>
          <el-button type="success" @click="addStudent" size="small" v-if="$hasBP('btn.Studen.add')">增加</el-button>
          <el-button type="success" @click="outExcelLIst" size="small"
            v-if="$hasBP('btn.studen.outExcel')">导出</el-button>
          <el-button type="success" @click="importExcel" size="small" v-if="$hasBP('btn.Studen.import')">导入</el-button>
          <!-- <el-button @click="clear" v-if="$hasBP('btn.Studen.select')" size="small">重置</el-button> -->
        </el-col>
      </el-row>



      <el-row style="margin-top: 10px;">
        <el-table ref="multipleTable" :data="tableData" style="width: 100%" border
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center">
          </el-table-column>
          <el-table-column prop="stuSno" fixed label="学号" width="120" align="center">
          </el-table-column>
          <el-table-column prop="stuName" label="姓名" width="120" align="center">
          </el-table-column>
          <el-table-column prop="teaName" label="辅导员姓名" width="120" align="center">
          </el-table-column>
          <el-table-column prop="claName" label="班级" width="120" align="center">
          </el-table-column>
          <el-table-column prop="stuSex" label="性别" width="80" align="center">
          </el-table-column>
          <el-table-column prop="stuDuties" label="职务" width="180" align="center">
          </el-table-column>
          <el-table-column prop="stuQq" label="QQ号" width="180" align="center">
          </el-table-column>
          <el-table-column prop="stuEmail" label="邮箱" width="200" align="center">
          </el-table-column>
          <el-table-column prop="stuPhone" label="手机号" width="180" align="center">
          </el-table-column>
          <el-table-column prop="stuPolitics" label="政治面貌" width="150" align="center">
          </el-table-column>
          <el-table-column prop="stuFamilyName" label="家长名称" width="130" align="center">
          </el-table-column>
          <el-table-column prop="stuFamilyPhone" label="家长电话" width="100" align="center">
          </el-table-column>
          <el-table-column prop="stuAddress" label="家庭地址" width="190" align="center">
          </el-table-column>
          <el-table-column prop="createName" label="创建人" width="100" align="center">
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="200" align="center">
            <template slot-scope="scope">{{ scope.row.createTime | formatCreateTimes }}</template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="200" fixed="right">
            <template #default="{row,$index}">
              <el-button type="warning" size="small" @click="updateClass(row)"
                v-if="$hasBP('btn.Studen.update')">修改</el-button>
              <el-button type="danger" size="small" @click="delTeacher(row)"
                v-if="$hasBP('btn.Studen.del')">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>


      <el-row>
        <div style="float: right;margin-top: 10px;">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="params.pageNum" :page-sizes="[1, 5, 10]" :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </el-row>
    </el-card>



    <el-dialog title="导入学生" :visible.sync="dialogExcel" width="60%">
      <el-row>
        <el-row>
          <span>1.上传文件</span>
          <el-upload class="upload-demo" drag :action="uploadUrl" :limit="1" :on-success="handleSuccess"
            :on-remove="handleRemove" :file-list="fileList">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-row>
        <el-row style="margin-top: 5px;">
          <span>2.文件浏览</span>
          <div style="margin: 10px 0px;"><el-button type="primary" size="small"
              @click="ShowImportList">查看导入数据</el-button></div>
          <el-table ref="multipleTable" :data="tableDataList" style="width: 100%" height="300" border
            v-loading="importLoading">
            <el-table-column prop="stuSno" fixed label="学号" width="120" align="center">
            </el-table-column>
            <el-table-column prop="stuName" label="姓名" width="120" align="center">
            </el-table-column>
            <el-table-column prop="className" label="班级" width="120" align="center">
            </el-table-column>
            <el-table-column prop="stuSex" label="性别" width="80" align="center">
            </el-table-column>
            <el-table-column prop="stuDuties" label="职务" width="180" align="center">
            </el-table-column>
            <el-table-column prop="stuQq" label="QQ号" width="180" align="center">
            </el-table-column>
            <el-table-column prop="stuEmail" label="邮箱" width="200" align="center">
            </el-table-column>
            <el-table-column prop="stuPhone" label="手机号" width="180" align="center">
            </el-table-column>
            <el-table-column prop="stuPolitics" label="政治面貌" width="150" align="center">
            </el-table-column>
            <el-table-column prop="stuFamilyName" label="家长名称" width="130" align="center">
            </el-table-column>
            <el-table-column prop="stuFamilyPhone" label="家长电话" width="100" align="center">
            </el-table-column>
            <el-table-column prop="stuAddress" label="家庭地址" width="190" align="center">
            </el-table-column>
          </el-table>
          <div style="color: red;margin-top: 10px;">请认真核对信息，不可出现重复数据，重复学号等~</div>
        </el-row>

      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogExcel = false">取 消</el-button>
        <el-button type="primary" @click="importStu">导入</el-button>
      </span>
    </el-dialog>



    <el-dialog title="学生操作" :visible.sync="isAddorUpdateShow" width="50%">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="学号" prop="stuSno">
          <el-input v-model="form.stuSno" :disabled="isAddOrUpdate==1" size="small"></el-input>
        </el-form-item>
        <el-form-item label="名字" prop="stuName">
          <el-input v-model="form.stuName" size="small"></el-input>
        </el-form-item>
        <el-form-item label="班级" prop="classId">
          <el-select v-model="form.classId" placeholder="请选择班级" style="width: 200px;" size="small" clearable
            :disabled="clasDis">
            <el-option v-for="item in ClassOptin" :key="item.id" :label="item.claName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="stuSex">
          <el-select v-model="form.stuSex" placeholder="请选择性别" style="width: 230px;" size="small">
            <el-option key="1" label="男" value="男">
            </el-option>
            <el-option key="2" label="女" value="女">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="QQ号">
          <el-input v-model="form.stuQq" size="small"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="stuEmail">
          <el-input v-model="form.stuEmail" size="small"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="stuPhone">
          <el-input v-model="form.stuPhone" size="small"></el-input>
        </el-form-item>
        <el-form-item label="政治面貌">
          <el-select v-model="form.stuPolitics" placeholder="请选择政治面貌" style="width: 230px;" size="small">
            <el-option v-for="item in stuPolitics" :key="item.id" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家长名称">
          <el-input v-model="form.stuFamilyName" size="small"></el-input>
        </el-form-item>
        <el-form-item label="家长电话">
          <el-input v-model="form.stuFamilyPhone" size="small"></el-input>
        </el-form-item>
        <el-form-item label="家庭地址">
          <el-cascader v-model="form.stuAddress" :options="addressTree" @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="职务" prop="stuDuties">
          <el-select v-model="form.stuDuties" placeholder="请选择职务" style="width: 230px;" size="small">
            <el-option v-for="item in stuDutiesOption" :key="item.id" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isAddorUpdateShow = false">取 消</el-button>
        <el-button type="primary" @click="affirm('form')">确 定</el-button>
      </span>
    </el-dialog>
    <iframe frameborder="0" name="downExcel" style="display:none"></iframe>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import {
    formatDate
  } from '../../../utils/date.js'
  export default {
    name: 'Student',
    data() {
      return {
        importLoading: false,
        addressTree: [],
        fileList: [],
        clasDis: false,
        dialogExcel: false,
        tableDataList: [],
        params: {
          pageNum: 1,
          pageSize: 5,
          keyWord: ""
        },
        selectCheckList: [],
        tableData: [],
        isAddOrUpdate: 0,//0添加1修改
        isAddorUpdateShow: false,
        form: {
          cteaSex: "",
          teaPolitics: ""
        },
        teacherList: [],
        total: 0,
        ishow: true,
        stuDutiesOption: [
          {
            id: 1,
            name: '班长'
          },
          {
            id: 2,
            name: '学习委员'
          },
          {
            id: 3,
            name: '体育委员'
          },
          {
            id: 4,
            name: '团委'
          },
          {
            id: 5,
            name: '纪律委员'
          },
          {
            id: 6,
            name: "普通学生"
          }
        ],
        stuPolitics: [
          {
            id: 1,
            name: "群众"
          },
          {
            id: 2,
            name: "共青团员"
          },
          {
            id: 3,
            name: "党员"
          },
        ],
        ClassOptin: [],
        rules: {
          stuSno: [
            { required: true, message: '请输入工号', trigger: 'blur' }
          ],
          stuName: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          stuSex: [
            { required: true, message: '请选择性别', trigger: 'change' }
          ],
          stuEmail: [
            { required: true, message: '请输入邮箱', trigger: 'blur' }
          ],
          stuPhone: [
            { required: true, message: '请输入手机号', trigger: 'blur' }
          ],
          stuDuties: [
            { required: true, message: '请选择职务', trigger: 'change' }
          ],
          classId: [
            { required: true, message: '请选择班级', trigger: 'change' }
          ]
        }
      };
    },
    computed: {
      ...mapGetters([
        'type',
        'detailId'
      ]),
      uploadUrl() {
        return process.env.VUE_APP_BASE_API + "/common/upload/file"
      }
    },
    mounted() {

      this.getList()
      this.getClassList()
      // 获取地址
      this.getAddresList()
    },
    filters: {
      formatCreateTimes(time) {
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd  hh:mm:ss')
      }
    },
    methods: {
      getClassList() {
        debugger
        let params = {}
        if (this.type == 0) {
          params.type = 0
          params.id = 0
        } else {
          params.type = this.type
          params.id = this.detailId
        }

        if (this.type !== 2) {
          this.$api.common.getClassList(params.type, params.id).then(res => {
            if (res.code == 200) {
              this.ClassOptin = res.data
            }
          })
        } else {
          this.clasDis = true
        }

      },
      getList() {
        debugger
        this.$api.student.getstudentList(this.params).then(res => {
          console.log(res);
          if (res.code == 200) {
            this.tableData = res.data
            this.total = res.pageParams.total
            this.ishow = false
          }
        })
      },
      affirm(formName) {
        let reg = /^1[13456789]\d{9}$/
        if (!reg.test(this.form.stuPhone)) {
          this.$message.error('学生手机号格式错误')
          return
        }
        if (!reg.test(this.form.stuFamilyPhone)) {
          this.$message.error('家长手机号格式错误')
          return
        }

        let arr = this.form.stuAddress
        this.form.stuAddress = `${arr[0]}-${arr[1]}-${arr[2]}`
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.isAddOrUpdate == 0) {//添加
              this.$api.student.addStudent(this.form).then(res => {
                if (res.code == 200) {
                  this.$message.success("添加成功~")
                  this.getList()
                  this.isAddorUpdateShow = false
                }
              })
            } else if (this.isAddOrUpdate == 1) {
              this.$api.student.updateStudent(this.form).then(res => {
                if (res.code = 200) {
                  this.$message.success("修改成功~")
                  this.getList()
                  this.isAddorUpdateShow = false
                }
              })
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      getAddresList() {
        this.$api.common.getAddressList().then(res => {
          if (res.code == 200) {
            this.addressTree = this.handelOption(res.data[0].children)
          }
        })
      },
      handelOption(options) {
        options.forEach((item, index) => {
          if (item.children.length == 0) {
            delete item.children;
          } else {
            this.handelOption(item.children);
          }
        });
        return options;
      },
      addStudent() {
        this.isAddOrUpdate = 0;
        this.isAddorUpdateShow = true;
        this.form = {}

      },
      updateClass(data) {
        console.log(data);
        this.form = JSON.parse(JSON.stringify(data))
        let arr = this.form.stuAddress.split("-")
        this.form.stuAddress = arr
        this.isAddorUpdateShow = true
        this.isAddOrUpdate = 1
      },
      handleSizeChange(val) {
        console.log(val);
        this.params.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.params.pageNum = val
        this.getList()
      },
      delTeacher(row) {
        // console.log(row);
        this.$confirm('此操作将永久删除数据,并且该学生将无法登录,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log(row);
          this.$api.student.delStudent(row.id).then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$message.success("删除成功~")
              this.getList()
            }
          })
        }).catch(() => { })
      },
      clear() {
        this.params = {}
        this.params.pageNum = 1
        this.params.pageSize = 10
        this.getList()
      },
      handleChange() {

      },
      outExcelLIst() {
        console.log(111);
        if (this.selectCheckList.length == 0) {
          this.$message.warning("请选择导出的数据~")
          return
        }
        let ids = []
        this.selectCheckList.forEach(item => {
          ids.push(item.id)
        })
        // console.log(ids.join(","));
        let iframe = window.frames['downExcel'];
        //console.log('iframe',iframe.location.href);
        let href = process.env.VUE_APP_BASE_API + "/excel/stu/exportOrder?ids=" + ids.join(",");
        //接口路径地址，返回数据类型为application/binary，后台控制显示信息，前端仅为下载功能
        iframe.location.href = href
      },
      handleSelectionChange(val) {
        this.selectCheckList = val
      },
      importExcel() {
        this.dialogExcel = true
      },
      handleSuccess(response, file, fileList) {
        this.fileList.push({ name: file.name, url: response.data })
      },
      handleRemove(file, fileList) {
        this.fileList = []
      },
      ShowImportList() {
        if (this.fileList.length == 0) {
          this.$message.warning("请先上传文件");
          return
        }
        this.$api.common.showExcelList({ filePath: this.fileList[0].url }).then(res => {
          if (res.code == 200) {
            this.tableDataList = res.data
          }
        })
      },
      importStu() {
        this.importLoading = true
        this.$api.common.ImportExcel({ filePath: this.fileList[0].url }).then(res => {
          if (res.code == 200) {
            this.getList()
          }
          this.dialogExcel = false
          this.importLoading = false
        })
      }

    },
  };
</script>

<style lang="scss" scoped>
  .content {
    padding: 10px;
  }

  .strong {
    font-weight: 600;
    font-size: 14px;
    color: #606266;
  }
</style>